package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.credit_bank.mapper.ArticlesMapper;
import csu.web.credit_bank.mapper.LikedMapper;
import csu.web.credit_bank.pojo.Articles;
import csu.web.credit_bank.pojo.Liked;
import csu.web.credit_bank.pojo.dto.PostCreateDto;
import csu.web.credit_bank.pojo.Posts;
import csu.web.credit_bank.service.PostsService;
import csu.web.credit_bank.mapper.PostsMapper;
import csu.web.credit_bank.service.StudentsService;
import csu.web.credit_bank.utils.UUIDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 于欣淇
* @description 针对表【posts(论坛主题表)】的数据库操作Service实现
* @createDate 2025-06-27 20:08:16
*/
@Service
public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts>
    implements PostsService{
    @Autowired
    private  UUIDManager uuidManager;
    @Autowired
    private StudentsService studentsService;
    @Autowired
    private LikedMapper likedMapper;
    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private PostsMapper postsMapper;

    public String createPost(PostCreateDto postDto){
        Posts post = new Posts();
        post.setTitle(postDto.getTitle());
        post.setContent(postDto.getContent());
        post.setFiltertype(postDto.getFiltertype());
        post.setAuthor(postDto.getAuthor());
        uuidManager.generateUUID(1000);
        post.setId(uuidManager.getUUID());
        post.setCreateTime(new Date());
        post.setLikes(0);
        this.save(post);
        return post.getId();
    }
    public List<Posts> getPostsList(String id){
        return baseMapper.selectPostsWithCommentCount(id);
    }
    @Override
    public void updatePostLikes(String postId,String userId) {
        QueryWrapper<Liked> likedQueryWrapper = new QueryWrapper<>();
        likedQueryWrapper.eq("post_id", postId);
        likedQueryWrapper.eq("user_id", userId);
        Liked existingLikedRecord = likedMapper.selectOne(likedQueryWrapper);
        Posts post  = this.getById(postId);
        if (post == null) {
            throw new IllegalArgumentException("帖子ID不存在：" + postId);
        }
        if (existingLikedRecord != null) {
            likedMapper.delete(likedQueryWrapper);
            post.setLikes(post.getLikes() - 1);
        } else {
            Liked newLikedRecord = new Liked();
            newLikedRecord.setPostId(postId);
            newLikedRecord.setUserId(userId);
            likedMapper.insert(newLikedRecord);
            post.setLikes(post.getLikes() + 1);
        }
        this.updateById(post);
    }
    @Override
    public int getTodayNum(String userId) {
        //找出该用户的所有文章和帖子
        List<Posts> postsList = postsMapper.selectList(new QueryWrapper<Posts>().eq("author",userId));
        List<Articles> articlesList = articlesMapper.selectList(new QueryWrapper<Articles>().eq("author_id",userId));
        int todayNum=0;
        for(Posts post:postsList){
            if(post.getCreateTime().after(new Date(System.currentTimeMillis()-24*60*60*1000))){
                todayNum++;
            }
        }
        for(Articles article:articlesList){
            if(article.getCreateTime().after(new Date(System.currentTimeMillis()-24*60*60*1000))){
                todayNum++;
            }
        }
        return todayNum;
    }
}